//
//  TileMenuButton.m
//  SAYAKA_Table
//
//  Created by numanuma08 on 2013/07/21.
//  Copyright (c) 2013年 勘米良. All rights reserved.
//

#import "TileMenuButton.h"

@implementation TileMenuButton
@synthesize title = _title;
@synthesize delegate = _delegate;
@synthesize category = _category;
@synthesize selected = _selected;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+ (id)tileMenuButton
{
    UINib *nib = [UINib nibWithNibName:@"TileMenuButton" bundle:nil];
    TileMenuButton *view = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
    [view initializeLayout];
    return view;
}

- (void)setTitle:(NSString *)title
{
    _titleLabel.text = title;
    _title = title;
}

- (void)setCategory:(Category *)category
{
    _category = category;
    self.title = category.name;
}

- (void)setSelected:(BOOL)selected
{
    _selected = selected;
    if (selected) {
        _titleLabel.textColor = [UIColor redColor];
    } else {
        _titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)initializeLayout
{
    _myFrame = self.frame;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.frame = CGRectMake(self.frame.origin.x+3, self.frame.origin.y+3,
                            self.frame.size.width, self.frame.size.height);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.frame = _myFrame;
    
    CGPoint point = [[touches anyObject] locationInView:self];
    if (0 < point.x && point.x <= self.frame.size.width &&
        0 < point.y && point.y <= self.frame.size.height) {
        if ([self.delegate respondsToSelector:@selector(tileManuButtonDidTouchUpInside:)]) {
            [self.delegate tileManuButtonDidTouchUpInside:self];
        }
    }
}
@end
